#include "StdAfx.h"
#include "LoanServerConfig.h"
#include "IniConfigUtil.h"

LoanServerConfig* LoanServerConfig::m_instance = nullptr;
CString LoanServerConfig::m_preParseCheckCode;

LoanServerConfig::LoanServerConfig(void)
{
	m_pAppName = _T("LoanConfig");
	m_loanServerPort = 5180;
	m_needSkin = false;
}

LoanServerConfig::~LoanServerConfig(void)
{
}

LoanServerConfig* LoanServerConfig::getInstance()
{
	if (nullptr == m_instance)
	{
		m_instance = new LoanServerConfig();
	}

	return m_instance;
}

bool LoanServerConfig::saveConfig()
{
	CIniConfigUtil* pconfigUtil = CIniConfigUtil::getInstance();
	BOOL res = 0;  
	//pconfigUtil->WriteIniConfigString(m_pAppName, "LoanServerIP", m_loanServerIP);
	res = pconfigUtil->WriteIniConfigString(m_pAppName, "LoginName", m_loginName);
	//res = pconfigUtil->WriteIniConfigString(m_pAppName, "LoginPwd", m_loginPwd);
	//TCHAR sztemp[10] = {0};
	//sprintf_s(sztemp, 10, "%d", m_loanServerPort);
	//res = pconfigUtil->WriteIniConfigString(m_pAppName, "LoanServerPort", sztemp);
	//res = pconfigUtil->WriteIniConfigString(m_pAppName, "LoginCode", m_loginCode);

	return res;
}

bool LoanServerConfig::readConfig()
{
	CIniConfigUtil* pconfigUtil = CIniConfigUtil::getInstance();
	LPTSTR strTemp = new TCHAR[100];
	DWORD rsize = 0; 
	pconfigUtil->GetIniConfigString(m_pAppName, "LoanServerIP", "", strTemp, 100);
	m_loanServerIP = strTemp;
	m_loanServerIP = m_loanServerIP.Trim();
	Config* pConfig = Config::getInstance();
	pConfig->m_httpBaseAddr = "http://" + m_loanServerIP;
#ifdef _DEBUG
	if (m_loanServerIP.Compare(_T("127.0.0.1")) == 0)
	{
		pConfig->m_httpBaseAddr = "http://112.74.215.145";
	}
#endif
	rsize = pconfigUtil->GetIniConfigString(m_pAppName, "LoanServerPort", "", strTemp, 100);
	if (strlen(strTemp) > 0)
	{
		m_loanServerPort = atoi(strTemp);
	}

	m_needSkin = false;
	rsize = pconfigUtil->GetIniConfigString(m_pAppName, "NeedSkin", "", strTemp, 100);
	if (strlen(strTemp) > 0)
	{
		CString strNeedSkin = strTemp;
		strNeedSkin = strNeedSkin.Trim();
		if (strNeedSkin.Compare("1") == 0)
		{
			m_needSkin = true;
		}
	}
	
	rsize = pconfigUtil->GetIniConfigString(m_pAppName, "LoginName", "", strTemp, 100);
	m_loginName = strTemp;
	//rsize = pconfigUtil->GetIniConfigString(m_pAppName, "LoginPwd", "", strTemp, 100);
	//m_loginPwd = strTemp;
	//rsize = pconfigUtil->GetIniConfigString(m_pAppName, "LoginCode", "", strTemp, 100);
	//m_loginCode = strTemp;
	rsize = pconfigUtil->GetIniConfigString(m_pAppName, "loginServerUrl", "http://112.74.215.145:5168/", strTemp, 100);
	m_loginServerUrl = strTemp;
	if ( m_loginServerUrl.GetAt(m_loginServerUrl.GetLength()-1) != _T('/') )
	{
		m_loginServerUrl += _T('/');
	}
	rsize = pconfigUtil->GetIniConfigString(m_pAppName, "HttpBaseAddr", "", strTemp, 100);
	if (strlen(strTemp) > 0)
	{
		pConfig->m_httpBaseAddr = strTemp;
	}
	if (pConfig->m_httpBaseAddr.GetAt(pConfig->m_httpBaseAddr.GetLength()-1) != _T('/') )
	{
		pConfig->m_httpBaseAddr += _T('/');
	}
	pConfig->initConfig();
	
	return (rsize > 0) ? true : false;
}

void LoanServerConfig::getLoginServerInfo(CString& lognCode)
{
	lognCode = lognCode.Trim();
	if (lognCode.GetLength() != 32)
	{
		return;
	}
	LoanServerConfig* loanConfig = LoanServerConfig::getInstance();
	if (loanConfig->m_loanServerIP.GetLength() > 5 && m_preParseCheckCode.Compare(lognCode) == 0)
	{
		return;
	}

	CString strGetLoginInfoUrl;
	strGetLoginInfoUrl.Format(_T("%sgetCompanyLoginInfo/?loginCode=%s"), loanConfig->m_loginServerUrl, lognCode);
	BYTE bResult[1024] = {0};
	int resSize = CHttpUtils::sendHttpRequest(strGetLoginInfoUrl, bResult, 1024);
	if (resSize > 0)
	{
		parseCompanyLoginInfo(bResult, lognCode);
		m_preParseCheckCode = lognCode;
	}
}

bool LoanServerConfig::parseCompanyLoginInfo(BYTE* pResult, CString& lognCode)
{
	Json::Reader reader;
	Json::Value jsonData;
	string strRevData((char*)pResult);
	if (!reader.parse(strRevData, jsonData) )
	{
		return false;
	}
	if (jsonData.size() == 0 || jsonData["res"].isNull())
	{
		return false;
	}
	CString strRes = jsonData["res"].asCString();
	if (strRes.Compare("success") == 0)
	{
		LoanServerConfig* loanConfig = LoanServerConfig::getInstance();
		loanConfig->m_loanServerIP = jsonData["serverIP"].asCString();
		loanConfig->m_loanServerPort = atoi(jsonData["port"].asCString());
		loanConfig->m_loginCode = lognCode.Trim();

		Config* pConfig = Config::getInstance();
		pConfig->m_httpBaseAddr = jsonData["httpServerAddr"].asCString();
		pConfig->m_httpBaseAddr = pConfig->m_httpBaseAddr.Trim();
		if (pConfig->m_httpBaseAddr.GetAt(pConfig->m_httpBaseAddr.GetLength()-1) != _T('/') )
		{
			pConfig->m_httpBaseAddr += _T('/');
		}
		pConfig->initConfig();

		return true;
	}

	return false;
}



